/* Copyright (c) 2004-2024 Peigen.info. All rights reserved. */

package info.peigen.hotpot.business.account.core.service;

import info.peigen.hotpot.business.account.facade.AccountFreezeServiceFacade;
import info.peigen.hotpot.business.account.facade.order.freeze.FreezeAmountOrder;
import info.peigen.hotpot.business.account.facade.order.freeze.UnfreezeAmountOrder;
import info.peigen.hotpot.business.account.facade.result.freeze.FreezeAmountResult;
import info.peigen.hotpot.business.account.facade.result.freeze.UnfreezeAmountResult;
import info.peigen.hotpot.common.service.base.processor.ProcessorServiceContext;
import info.peigen.hotpot.common.service.base.processor.ProcessorServiceInvoke;
import org.noear.solon.annotation.Component;

/**
 * <b>(AccountFreezeService)</b>
 *
 * @author Peigen
 * @version 1.0.0
 * @since 2021/7/29
 */
@SuppressWarnings("rawtypes")
@Component
public class AccountFreezeService extends AbstractAccountService implements AccountFreezeServiceFacade {

    @Override
    public FreezeAmountResult freezeAmount(FreezeAmountOrder order) {
        ProcessorServiceContext serviceContext = ProcessorServiceContext.builder().order(order).result(new FreezeAmountResult()).build();
        execute(serviceContext,
                new ProcessorServiceInvoke() {
                    @Override
                    public void addPreProcessor(ProcessorServiceContext serviceContext) {
                        serviceContext.addProcessors(activeAccountProcessor);
                    }

                    @Override
                    public void addProcessor(ProcessorServiceContext serviceContext) {
                        serviceContext.addProcessors(freezeAmountProcessor);
                    }

                    @Override
                    public void addPostProcessor(ProcessorServiceContext serviceContext) {
                        serviceContext.addProcessors(storeAccountProcessor, storeAccountFreezeLogProcessor);
                    }
                });
        return (FreezeAmountResult) serviceContext.getResult();
    }

    @Override
    public UnfreezeAmountResult unfreezeAmount(UnfreezeAmountOrder order) {
        ProcessorServiceContext serviceContext = ProcessorServiceContext.builder().order(order).result(new UnfreezeAmountResult()).build();
        execute(serviceContext,
                new ProcessorServiceInvoke() {
                    @Override
                    public void addPreProcessor(ProcessorServiceContext serviceContext) {
                        serviceContext.addProcessors(activeAccountProcessor);
                    }

                    @Override
                    public void addProcessor(ProcessorServiceContext serviceContext) {
                        serviceContext.addProcessors(unfreezeAmountProcessor);
                    }

                    @Override
                    public void addPostProcessor(ProcessorServiceContext serviceContext) {
                        serviceContext.addProcessors(storeAccountProcessor, storeAccountFreezeLogProcessor);
                    }
                });
        return (UnfreezeAmountResult) serviceContext.getResult();
    }
}
